package com.dxjy.demo.module.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxjy.demo.entity.R;
import com.dxjy.demo.module.sys.entity.SysUser;
import com.dxjy.demo.module.sys.mapper.SysUserMapper;
import com.dxjy.demo.utils.page.PageResult;
import com.dxjy.demo.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@CrossOrigin
@Api(tags = "demo")
@RestController
@RequestMapping("/demo")
public class DemoController {

	@Resource
	private SysUserMapper userMapper;


	@ApiOperation(value = "分页demo")
	@GetMapping(path = "/page")
	public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<SysUser> list = userMapper.getUserList();
		PageInfo pageInfo = new PageInfo<>(list);
		PageResult pageResult = PageUtils.getPageResult(pageNum, pageSize, pageInfo);
		return R.ok(pageResult);
	}

	@ApiOperation(value = "MyBatisPlus demo")
	@GetMapping(path = "/mybatisplus")
	public R mybatisplus(@RequestParam Integer id) {

		//QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
		//wrapper.eq("username", "alex");
		//Integer count = userMapper.selectCount(wrapper);
		LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysUser::getPhone, "");
		SysUser user = userMapper.selectOne(queryWrapper);
		//SysUser user = userMapper.getUserByUserId(1);
		return R.ok(user);
	}

	@ApiOperation(value = "exception")
	@GetMapping(path = "/ex")
	public R test() {
		String str = null;
		if (str.equals("123")) {
			System.out.println("+++");
		}
		return R.ok();
	}


	@Cacheable(value = "testCache")
	@ApiOperation(value = "缓存")
	@GetMapping(path = "/cache")
	public R cache(@RequestParam int id) {
		System.out.println("=============> 业务查询");
		JSONObject result = new JSONObject();
		result.put("id", id);
		result.put("name", id + 1000);
		return R.ok(result);
	}


}
